/**
 * 
 */
package com.example.radioclub.login;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.client.RestTemplate;

import com.example.radioclub.R;
import com.example.radioclub.data.Constants;
import com.example.radioclub.interfaces.http.IHttpImageDownloader;
import com.example.radioclub.interfaces.http.IHttpJSONDownloader;
import com.example.radioclub.login.helper.IUserRadioAuthTask;
import com.example.radioclub.login.helper.UserRadioAuthTask;
import com.example.radioclub.music.MusicActivity;
import com.example.radioclub.music.interfaces.IRestMusic;
import com.example.radioclub.music.tasks.RestClientAuthDownloader;
import com.example.radioclub.tasks.http.HttpImageDownloader;
import com.facebook.Session;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This class enclosed all login methods possible. In this case: Facebook and
 * Google, for now. This class uses FacebookLoginActivity class, 
 * GoogleLoginActivity class, YahooLoginAcivityClass, and MsnLoginActivityClass.
 * 
 * @author wizard
 * 
 */
public class LoginActivity extends Activity implements OnClickListener, IUserRadioAuthTask, IRestMusic {
	private final String TAG = Constants.APP_TAG + getClass().getName();

	private Bundle bundle = null;
	private ImageView profileImage;
	private TextView profileName, profileEmail, profileExpiredDate, profileMemberSince, profileMembership;
	private Button goToClubButton;
	private UserRadioAuthTask melOnUserAuthenticationTask;
	private RestClientAuthDownloader melOnClientAuthenticationTask;
	private ProgressDialog progressDialog;
	private String melOnUserId = null, melOnUserCookie = null, melOnClientCookie = null,
			melOnChannelCd = null, socmedId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i(TAG, "Begin Login Activity");

		this.bundle = getIntent().getExtras();

		setContentView(R.layout.activity_main_page);
		this.profileImage = (ImageView) findViewById(R.id.profile_photo_box);
		this.profileName = (TextView) findViewById(R.id.profile_username_box);
		this.profileEmail = (TextView) findViewById(R.id.profile_email_box);
		this.profileExpiredDate = (TextView) findViewById(R.id.profile_membership_card_expire);
		this.profileMemberSince = (TextView) findViewById(R.id.profile_membership_since);
		this.profileMembership = (TextView) findViewById(R.id.profile_membership_type);
		this.goToClubButton = (Button) findViewById(R.id.goto_club_page);
		this.goToClubButton.setOnClickListener(this);
		
		if (Constants.USER_LOGIN_STATUS == false) {
			if (bundle.getInt("login") == Constants.FACEBOOK_LOGIN) {
				Log.i(TAG, "Login with Facebook.");
				startActivityForResult(new Intent(this, FacebookLoginActivity.class), 0);
			} else if (bundle.getInt("login") == Constants.GOOGLE_LOGIN) {
				Log.i(TAG, "Login with Google.");
				startActivityForResult(new Intent(this, GoogleLoginActivity.class), 0);
			} else if (bundle.getInt("login") == Constants.MSN_LOGIN) {
				Log.i(TAG, "Login with MSN Account.");
				startActivityForResult(new Intent(this, MsnLoginActivity.class), 0);
			} else if (bundle.getInt("login") == Constants.YAHOO_LOGIN) {
				Log.i(TAG, "Login with Yahoo Account.");
				startActivityForResult(new Intent(this, GoogleLoginActivity.class), 0);
			} else {
				// Yahoo and MSN are not yet implemented, please come back later
			}
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		Log.i(TAG, "onResume()");
		super.onResume();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(resultCode == RESULT_OK) {
			if(data != null) {
				this.socmedId = data.getExtras().getString("id");
				this.profileName.setText(data.getExtras().getString("name"));
				this.profileEmail.setText(data.getExtras().getString("email"));
				
				if(Constants.CLIENT_LOGIN_STATUS == false || Constants.USER_LOGIN_STATUS == false)  {
					this.progressDialog = ProgressDialog.show(this, "Please wait.", "Authenticating...");
					// Authenticating MelOn client returning cookie
					if(Constants.CLIENT_LOGIN_STATUS == false) {
						this.melOnClientAuthenticationTask = new RestClientAuthDownloader(Constants.API_MELON_CLIENTNAME, Constants.API_MELON_PASSWORD, this);
						this.melOnClientAuthenticationTask.execute();
					}
					// Authenticating MelOn user, returnig cookie
					if(Constants.USER_LOGIN_STATUS == false) {
						this.melOnUserAuthenticationTask = new UserRadioAuthTask(data.getExtras().getInt("login"), this.socmedId, this);
						this.melOnUserAuthenticationTask.execute();
					}
					
				}					
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.goto_club_page:
			if(Constants.CLIENT_LOGIN_STATUS = true && Constants.USER_LOGIN_STATUS == true) {
				Bundle bundle = new Bundle();
				bundle.putString("melon_user_id", this.melOnUserId);
				bundle.putString("melon_channel_code", this.melOnChannelCd);
				bundle.putString("melon_user_cookie", this.melOnUserCookie);
				bundle.putString("melon_client_cookie", this.melOnClientCookie);
				intent = new Intent(this, MusicActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
				Toast.makeText(this, "Go to Club!", Toast.LENGTH_LONG).show();				
			} else {
				Toast.makeText(this, "Oops! You haven't logged in, have you?", Toast.LENGTH_SHORT).show();
			}
			break;
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Constants.CLIENT_LOGIN_STATUS = false;
		setResult(RESULT_OK, new Intent());
		super.onBackPressed();
	}

	@Override
	public void onUserAuthenticationFinished(String result) {
		// TODO Auto-generated method stub
		if(this.melOnClientAuthenticationTask.getStatus() == Status.FINISHED) {
			if(this.progressDialog.isShowing()) this.progressDialog.dismiss();
		}
		if(result != null) {
			Constants.USER_LOGIN_STATUS = true;
			this.melOnUserCookie = result;	
			this.bundle.putString("melon_user_cookie", this.melOnUserCookie);
			
		}
	}

	@Override
	public void onCertificateDownloadFinished(String result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClientAuthenticationFinished(Object[] result) {
		// TODO Auto-generated method stub
		if(this.melOnUserAuthenticationTask.getStatus() == Status.FINISHED) {
			if(this.progressDialog.isShowing()) this.progressDialog.dismiss();
		}
		if(result != null) {
			this.melOnClientCookie = (String) result[2];
			this.melOnChannelCd = (String) result[1];
			this.bundle.putString("melon_client_cookie", this.melOnClientCookie);
			this.bundle.putString("melon_channel_code", this.melOnChannelCd);
		}
	}

	@Override
	public void onDownloadFinished(Object object) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGenreDownloadFinished(String result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMusicFinished(String result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSearchingFinished(String result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onServerAuthFinished(RestTemplate result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUserDetailDownloadFinished(String result) {
		// TODO Auto-generated method stub
		
	}
}
